package com.bwie.user.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.User;
import com.bwie.common.result.Result;
import com.bwie.common.utils.JwtUtils;
import com.bwie.user.mapper.UserMapper;
import com.bwie.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author：杨鹏
 * @Package：com.bwie.user.service.impl
 * @Project：yp_zhoukao723
 * @name：UserServiceImpl
 * @Date：2024/7/23 9:18
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Result<User> login(User user) {
        User user1 = userMapper.login(user);
        return Result.success(user1);
    }

    @Override
    public Result listUserId() {
        String token = request.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        String user = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        User user1 = JSONObject.parseObject(user, User.class);
        User login = userMapper.login(user1);
        return Result.success(login);
    }

    @Override
    public Result<User> updUser(User user) {
        Integer i = userMapper.updUser(user);
        return i>0?Result.success():Result.error();
    }
}
